package com.kaifamiao.dispatch;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

@WebServlet( "/user/sign/in" )
public class UserSignInServlet extends HttpServlet {

    private Logger logger = Logger.getLogger( this.getClass().getName() );

    @Override
    protected void service(HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {
        logger.info( "处理用户登录请求" );

        String username = request.getParameter( "username" );
        String password = request.getParameter( "password" );

        if( "老王".equals( username ) && "hello".equals( password ) ) {
            String path = "/WEB-INF/vip.html" ;
            // 获得 请求调度器 ( 请求指派器 、 请求派遣器 )
            RequestDispatcher rd = request.getRequestDispatcher( path );
            rd.forward( request , response );
            return ;
        }

        response.setCharacterEncoding( "UTF-8" );
        response.setContentType( "text/html;charset=UTF-8" );
        PrintWriter out = response.getWriter();
        out.println( "<h1>登录失败</h1>" );
        out.println( "<a href=\"/sign-in.html\">重新登录</a>" );

    }
}
